Skip to content

Fix async session handling by separating sync and async test execution#40

Closed
Copilot wants to merge 7 commits into
ci/add-ast-based-script-for-async-methodsfrom
copilot/sub-pr-36-yet-again
Closed

Fix async session handling by separating sync and async test execution#40
Copilot wants to merge 7 commits into
ci/add-ast-based-script-for-async-methodsfrom
copilot/sub-pr-36-yet-again

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jan 6, 2026

Description

When PGMQueue is initialized with async DSN/engine/session_maker, the _check_pgmq_ext() and _check_pg_partman_ext() methods were attempting to use sync context managers with AsyncSession objects, causing TypeError: 'AsyncSession' object does not support the context manager protocol.

Changes

Following feedback to maintain strict separation between sync and async operations, the fix was implemented on the test side:

  • Fixed _check_pgmq_ext() and _check_pg_partman_ext(): Added async detection logic that routes to async versions (_check_pgmq_ext_async(), _check_pg_partman_ext_async()) when is_async is True
  • Created async versions of check methods: Prevents nested event loop issues by using await in async contexts
  • Updated async methods: create_partitioned_queue_async() and drop_queue_async() now use await when calling async check methods
  • Created test helper utility: Added call_method() in tests/_async_utils.py that automatically routes to sync or async methods based on the PGMQueue instance type
  • Updated test fixtures and tests: All fixtures (pgmq_setup_teardown, pgmq_partitioned_setup_teardown) and test methods now properly handle both sync and async PGMQueue instances using the call_method() helper

This approach maintains the architectural separation between _execute_operation (sync) and _execute_async_operation (async) while ensuring all tests work correctly with both sync and async PGMQueue instances.

Example

# Tests now work with both sync and async PGMQueue instances
def test_create_queue(pgmq_all_variants, db_session):
    pgmq = pgmq_all_variants
    queue_name = f"test_queue_{uuid.uuid4().hex}"
    # Automatically calls create_queue() or create_queue_async() based on pgmq.is_async
    call_method(pgmq, 'create_queue', queue_name)
    assert check_queue_exists(db_session, queue_name) is True

Status

  • In progress
  • Ready for review
  • Done

Checklist

  • Read the Contributing Guide
  • Passes tests
  • Linted ( we use pre-commit with ruff )
  • Updated documentation

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits January 6, 2026 05:49
Co-authored-by: jason810496 <68415893+jason810496@users.noreply.github.com>
Co-authored-by: jason810496 <68415893+jason810496@users.noreply.github.com>
Copilot AI changed the title [WIP] Address test issues for async methods script Fix async session handling in PGMQueue operations Jan 6, 2026
Copilot AI requested a review from jason810496 January 6, 2026 05:57
Copy link
Copy Markdown
Owner

@jason810496 jason810496 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot

We should fix on the test side instead of _execute_operation, as we had already separate _execute_operation and _execute_async_operation.

Co-authored-by: jason810496 <68415893+jason810496@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Jan 6, 2026

@copilot

We should fix on the test side instead of _execute_operation, as we had already separate _execute_operation and _execute_async_operation.

I've reverted the changes to _execute_operation and fixed the issue on the test side instead.

Changes made:

  • Reverted _execute_operation to maintain strict separation between sync and async operations
  • Fixed _check_pgmq_ext and _check_pg_partman_ext to properly handle async PGMQueue instances by checking is_async flag
  • Updated test fixtures (pgmq_setup_teardown, pgmq_partitioned_setup_teardown) to use async methods when dealing with async instances
  • Created call_method() helper utility in tests/_async_utils.py that automatically calls the appropriate sync or async method
  • Updated all test methods in test_queue.py to use the call_method() helper

All tests are now passing. Fixed in commit 889308c.

Copilot AI and others added 2 commits January 6, 2026 06:10
Co-authored-by: jason810496 <68415893+jason810496@users.noreply.github.com>
Co-authored-by: jason810496 <68415893+jason810496@users.noreply.github.com>
Copilot AI changed the title Fix async session handling in PGMQueue operations Fix async session handling by separating sync and async test execution Jan 6, 2026
Copilot AI requested a review from jason810496 January 6, 2026 06:13
@jason810496 jason810496 closed this Jan 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants